
#include <stdlib.h>
#include <signal.h>
#include "frontend.h"
#include "getop.h"
#include "client_session_layer.h"
#include "client_backend.h"

/* process input and request calculation to the server */
int 
do_client(void)
{
	formulaT formula;
	int ret = CONTINUE;

	frontend(&formula);

	switch(formula.opcode)
	{
		case ADD:	
			formula.ans = add(formula.op1, formula.op2); 
			break;
		case SUB:	
			formula.ans = sub(formula.op1, formula.op2); 
			break;
		case MULT:	
			formula.ans = mult(formula.op1, formula.op2); 
			break;
		case DIV:	
			formula.ans = divide(formula.op1, formula.op2); 
			break;
		case EXIT:	
			ret = QUIT;
			break;
		default: break;
	}
	
	if (ret != QUIT) 
		print_answer(&formula);
	return ret;
}

/* set signals handler */
void
setSignalCatcher(void)
{
	signal(SIGINT, exit);
	signal(SIGTERM, exit);
	signal(SIGQUIT, exit);
	signal(SIGALRM, exit);
}

/* makes an add operation */
double 
add(double op1, double op2)
{
	return doOperation(op1, op2, ADD_OP);
}

/* makes a substract operation */
double 
sub(double op1, double op2)
{
	return doOperation(op1, op2, SUB_OP);
}

/* makes a multiplication operation */
double 
mult(double op1, double op2)
{
	return doOperation(op1, op2, MULT_OP);
}

/* makes a division operation */
double 
divide(double op1, double op2)
{
	return doOperation(op1, op2, DIV_OP);
}
